package com.zoespeer.food_delivery_comment_api.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.Data;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@TableName("users") // 映射数据库 users 表
@Data
public class User {

    @TableId(value = "user_id", type = IdType.AUTO) // 主键 user_id，自增
    private Integer userId;


    // 唯一约束：将 @TableUnique 与 @TableField 结合，用数组格式声明
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)

    private String email;

    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String nickname; // 昵称

    @TableField("password_hash") // 映射数据库 password_hash 字段
    private String password; // 密码（存储 BCrypt 加密后的值）

    @TableField("avatar_url")
    private String avatarUrl; // 头像地址

    private String status; // 状态（0=正常，1=封禁）

    @TableField("created_at")
    private LocalDateTime createdAt; // 创建时间

    @TableField("updated_at")
    private LocalDateTime updatedAt; // 更新时间

    // 自动填充处理器
    @Component
    public static class UserMetaObjectHandler implements MetaObjectHandler {
        @Override
        public void insertFill(MetaObject metaObject) {
            strictInsertFill(metaObject, "createdAt", LocalDateTime.class, LocalDateTime.now());
            strictInsertFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());
        }

        @Override
        public void updateFill(MetaObject metaObject) {
            strictUpdateFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());
        }
    }
}